﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.GetSession
{
    public static class ControllerBaseGetSession
    {
		/// <summary>
		/// 拓展方法：获取当前登陆的用户信息
		/// </summary>
		/// <param name="controllerBase"></param>
		/// <returns></returns>
		public static UserInfo GetSessionUserInfo(this ControllerBase controllerBase)
		{
			var userInfoJson = controllerBase.HttpContext.Session.GetString("UserInfo");
			if (string.IsNullOrEmpty(userInfoJson))
			{
				return null;
			}
			//反序列化
			UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(userInfoJson);
			return userInfo;
		}
	}
}
